package com.gyw.controller;


import com.gyw.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
@CrossOrigin
public class OssController {


    @Autowired
    OssUtil ossUtil;

    @PostMapping("/upload")
    //MultipartFile接口用来接收前端传来的文件  file是选中的文件  getOriginalFilename()用于获取客户端上传文件的原始文件名
    public String upload(MultipartFile file) throws Exception {
        String url = ossUtil.upload(getNewFileName(file.getOriginalFilename()), file.getInputStream());
        return url;

    }

    String getNewFileName(String oldFileName)
    {
        //获取附件的后缀
        String sufix=getFileSufix(oldFileName);
        //新的文件名
        String newFileName=System.currentTimeMillis()+sufix;
        return newFileName;
    }


    String getFileSufix(String filename){
        return filename.substring(filename.lastIndexOf("."));
    }



}
